package com.ruoyi.haircut.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.haircut.domain.Member;
import com.ruoyi.haircut.domain.OrderStatistics;
import com.ruoyi.haircut.service.IComboService;
import com.ruoyi.haircut.service.IMemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.haircut.domain.Order;
import com.ruoyi.haircut.service.IOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 结账Controller
 *
 * @author ruoyi
 * @date 2023-05-13
 */
@RestController
@RequestMapping("/haircut/order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IMemberService menuService;

    @Autowired
    private IComboService comboService;

    @PostMapping("/OrderStatistics")
    public AjaxResult getOrderStatistics(@RequestBody OrderStatistics orderStatistics) {
        Map<String, Object> map = orderService.getOrderStatistics(orderStatistics);
        return AjaxResult.success(map);
    }

    /**
     * 查询结账列表
     */
    @PreAuthorize("@ss.hasPermi('haircut:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出结账列表
     */
    @PreAuthorize("@ss.hasPermi('haircut:order:export')")
    @Log(title = "结账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "结账数据");
    }

    /**
     * 获取结账详细信息
     */
    @PreAuthorize("@ss.hasPermi('haircut:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderService.selectOrderById(id));
    }

    /**
     * 新增结账
     */
    @Transactional
    @PreAuthorize("@ss.hasPermi('haircut:order:add')")
    @Log(title = "结账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)

    {

        logger.error(order.toString());
        //1.会员支付
        Long memberId = order.getMemberId();
        order.setCreateUser(getUsername());
        order.setUpdateUser(getUsername());

        order.setComboName(comboService.selectComboById(order.getComboId()).getName());
        if (memberId != null ) {
            //如果是会员支付需要扣减账户的余额，并且需要判断余额是否充足
            Member member = menuService.selectMemberById(memberId);
            BigDecimal money = member.getMoney();
            int i = money.compareTo(order.getPayMoney());
            if (i < 0) {
                throw new RuntimeException("账户余额不足，请充值！");
            }
            BigDecimal origMoney = member.getMoney();
            member.setMoney(origMoney.subtract(order.getPayMoney()));
            menuService.updateMember(member);
            order.setName(menuService.selectMemberById(order.getMemberId()).getName());
            order.setPayMethod(0L);
            order.setMemberId(memberId);

            return toAjax(orderService.insertOrder(order));
        }
        //2.如果是普通用户支付
        String name = order.getName();
        if (name == null) {
            order.setName("普通顾客");
        }
        order.setPayMethod(1L);
        return toAjax(orderService.insertOrder(order));

    }

    /**
     * 修改结账
     */
    @PreAuthorize("@ss.hasPermi('haircut:order:edit')")
    @Log(title = "结账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除结账
     */
    @PreAuthorize("@ss.hasPermi('haircut:order:remove')")
    @Log(title = "结账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }
}
